package com.fanben100;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.fanben100.gui.TerminalFrame;

public class Main implements Runnable {

	// private boolean packFrame = true;

	// public Main() {
	//
	// JFrame frame = new TerminalFrame();
	// // Validate frames that have preset sizes
	// // Pack frames that have useful preferred size info, e.g. from their
	// // layout
	// if (packFrame) {
	// frame.pack();
	// } else {
	// frame.validate();
	// }
	//
	// // Center the window
	// Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	// Dimension frameSize = frame.getSize();
	// if (frameSize.height > screenSize.height) {
	// frameSize.height = screenSize.height;
	// }
	// if (frameSize.width > screenSize.width) {
	// frameSize.width = screenSize.width;
	// }
	// frame.setLocation((screenSize.width - frameSize.width) / 2,
	// (screenSize.height - frameSize.height) / 2);
	// // 全屏
	// // frame.setUndecorated(true);// 禁用frame的装饰,即没有标题栏等
	// // Toolkit tk = Toolkit.getDefaultToolkit();
	// // Dimension di = tk.getScreenSize();
	// // frame.setSize(di);
	// frame.getContentPane().setVisible(false);
	// frame.setVisible(true);
	// RegistContext registContext = RegistContext.getInstance();
	//
	// // 检查是否注册
	// registContext.checkRegist(frame);
	//
	// }

	public static void main(String[] args) {
		// try {
		// JFrame.setDefaultLookAndFeelDecorated(true);
		// // UIManager.setLookAndFeel(new
		// // SubstanceOfficeBlue2007LookAndFeel());
		// } catch (Exception e) {
		// System.out.println("Substance Raven Graphite failed to initialize");
		// }
		// SwingUtilities.invokeLater(new Runnable() {
		// public void run() {
		// try {
		// // UIManager.setLookAndFeel(UIManager
		// // .getSystemLookAndFeelClassName());
		// JFrame.setDefaultLookAndFeelDecorated(true);
		// } catch (Exception exception) {
		// exception.printStackTrace();
		// }
		// try {
		// new Main();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }
		// });
		 try {
			UIManager.setLookAndFeel(UIManager
						 .getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		ThreadGroup errorGroup = new ThreadGroup("errors");

		SplashWindow window = new SplashWindow(
				Main.class.getResource("/logo.gif"), null);

		window.setVisible(true);

		Main main = new Main();

		Thread initThread = new Thread(errorGroup, main,
				"BroadCasterInitThread");

		try {
			Thread.sleep(1000);
		} catch (Exception e) {
		}
		initThread.start();
		window.setVisible(false);
	}

	/**
	 * 闪屏启动窗口
	 * 
	 * @author Administrator
	 * 
	 */
	private static class SplashWindow extends JWindow {
		public SplashWindow(URL filename, Frame f) {
			super(f);
			JLabel l = new JLabel(new ImageIcon(filename));

			getContentPane().add(l, BorderLayout.CENTER);
			pack();

			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			Dimension labelSize = l.getPreferredSize();

			setLocation((screenSize.width / 2) - (labelSize.width / 2),
					(screenSize.height / 2) - (labelSize.height / 2));
			setVisible(true);
			screenSize = null;
			labelSize = null;
			this.toFront();
		}
	}

	private void startMain() {
		// RegistContext.getInstance().checkRegist(
		// TerminalFrame.getPosFrame());
		ThreadGroup errorGroup = new ThreadGroup("errors"); //$NON-NLS-1$
		new Thread(errorGroup, "PosFrameThread") { //$NON-NLS-1$
			public void run() {
				TerminalFrame.openPosFrame();
			}
		}.start();
	}

	public void start() {
		Runnable callBack = new Runnable() {
			public void run() {
				startMain();
			}
		};
		callBack.run();
	}

	public void run() {
		TerminalFrame.openPosFrame();
		//start();
	}
}
